from fastapi import FastAPI
from routes.user import user_router
from routes.journal import journal_router
from database import init_db
from fastapi.middleware.cors import CORSMiddleware

app = FastAPI()
init_db()

# 添加 CORS 支持 - 支持8081端口访问
app.add_middleware(
    CORSMiddleware,
    allow_origins=[
        "http://localhost:3000",
        "http://localhost:8080",  # SSH隧道前端端口
        "http://127.0.0.1:3000",
        "http://127.0.0.1:8080",   # SSH隧道前端端口
        "http://8.130.47.217:3000",  # 服务器直接访问
        "https://8.130.47.217:3000", # HTTPS支持
    ],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

@app.get("/")
def read_root():
    return {"message": "Journal AI Assistant is running!"}

app.include_router(journal_router, prefix="/api/journal", tags=["Journal"])
app.include_router(user_router, prefix="/api/journal", tags=["User"])
